package internal

//机器人配置，通用

import (
	"lol.com/feb214-game/nest.git/log"
	"lol.com/feb214-game/nest.git/tools/sample"
	"lol.com/feb214-game/nest.git/tools/tz"
	"math/rand"
	"strconv"
	"talent.com/cube/conf/g"
)

type robotLevelConfig struct {
	InitCredit []int64
	ResetEdge  []int64
	Weight     int
}
type robotChipWeightConfig struct {
	Range  []int64
	Weight []int
	Rate   float32
}
type RobotConfig struct {
	ActiveCount          []int
	LevelConfig          []*robotLevelConfig
	OverCreditSleepRange []int
	TermQuitRate         float32
	TermQuitSleepRange   []int
	ChipWeight           []*robotChipWeightConfig
	ChipBetCount         [][]int
	BotIndexes           []int32
	BetIndexRate         map[string]int
	BetIndexReChooseRate float32
	WorkTimeLimit        int64
	OverWorkSleepRate    float32
	OverWorkSleepRange   []int
	ActiveRate           []float32

	weightArray []int
	indexArray  []int32
}

func (rc *RobotConfig) CurrentMaxActiveCount(roomKind int32) int {
	h := tz.LocalNow().Hour()
	//log.Info("hour:%v ActiveCount:%v ActiveRate:%v", h, rc.ActiveCount[roomKind], rc.ActiveRate[h])
	return int(float32(rc.ActiveCount[roomKind]) * rc.ActiveRate[h])
}

func (rc *RobotConfig) GetRandomLevel() int {
	weight := make([]int, len(rc.LevelConfig))
	for i, lc := range rc.LevelConfig {
		weight[i] = lc.Weight
	}
	return sample.WeightedChoice(weight)
}

//根据用户金额和配置随机出筹码索引，-1表示不下注
func (rc *RobotConfig) RandChipIndexByCredit(credit int64) int {
	credit /= g.CoinRate
	for _, cf := range rc.ChipWeight {
		if credit >= cf.Range[0] && credit <= cf.Range[1] {
			if rand.Float32() > cf.Rate {
				return -1
			}
			return sample.WeightedChoice(cf.Weight)
		}
	}
	return -1
}

// 随机下注索引
func (rc *RobotConfig) RandBetIndex() int32 {
	var sumRate int
	for _, rate := range rc.BetIndexRate {
		sumRate += rate
	}
	ra := rand.Intn(sumRate)
	for i, rate := range rc.BetIndexRate {
		if ra < rate {
			idx, err := strconv.Atoi(i)
			if err != nil {
				log.Error("can't convert %v to int, please check robot conf", i)
				continue
			}
			return int32(idx)
		}
		ra -= rate
	}
	return -1
}

// 根据牌型动态调整各区域下注的概率
func (rc *RobotConfig) RandBetIndexByCards(betidxRate []int32) int32 {
	if len(rc.weightArray) == 0 {
		rc.weightArray = make([]int, len(rc.BetIndexRate))
		rc.indexArray = make([]int32, len(rc.BetIndexRate))
		var (
			err error
			idx int
		)

		for index, weight := range rc.BetIndexRate {
			idx, err = strconv.Atoi(index)
			if err != nil {
				log.Error("can't convert %v to int, please check robot conf", index)
				continue
			}

			if idx < len(betidxRate) {
				weight += int(betidxRate[idx])
			}

			rc.weightArray[idx] = weight
			rc.indexArray[idx] = int32(idx)
		}
	}
	index := sample.WeightedChoice(rc.weightArray)
	return rc.indexArray[index]
}

func (rc *RobotConfig) ResetBetWeight() {
	rc.indexArray = nil
	rc.weightArray = nil
}

//根据筹码索引随机下注次数
func (rc *RobotConfig) RandBetCountByChip(chipIndex int) int {
	rg := rc.ChipBetCount[chipIndex]
	return sample.RandInt(rg[0], rg[1])
}
